package com.wuchao.servlet;

import com.wuchao.service.serviceimpl.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 吴超66
 * @version 1.0
 * @date 2022/4/19 09:20
 */
@WebServlet(name = "Servlet143")
public class Servlet143 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        ServiceImpl service = new ServiceImpl();
        String op=(String)request.getParameter("op");
        if(op!=null){
            String name=request.getParameter("name");
            String pwd=request.getParameter("pwd");//表单默认传值String
            if(op.equals("load")){
                int loading = service.isLoading("select count(1) from t143 where name=? and age=?", name, pwd);
                if(loading>0){//永远看不到，不要在这里写输出语句，还有一旦转走、响应那就走，剩余代码
                    //会话 设置1分钟免登录
                    session.setMaxInactiveInterval(60);
                    session.setAttribute("login",name);
                    //cookie 用来做非法入侵的
           /*     Cookie cookie=new Cookie("login",name);
                cookie.setMaxAge(45);
                response.addCookie(cookie);//只能往后页面查询*/
                    request.getRequestDispatcher("main.jsp").forward(request,response);
                }else{
                    response.sendRedirect("load.jsp?isloading=false");
                }
            }else if(op.equals("register")){
                int pwd1=Integer.parseInt(pwd);
                int registering = service.update("insert t143(name,sex,age) values(?,'男',?) ", name, pwd1);
                if(registering>0){//永远看不到，不要在这里写输出语句，还有一旦转走、响应那就走，剩余代码
                    request.getRequestDispatcher("load.jsp?registering1=true").forward(request,response);
                }else{
                    response.sendRedirect("load.jsp?registering2=false");
                }
            }else{

            }

        }else{
            response.sendRedirect("load.jsp?illegal=true");
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
